package com.k12.user.service.impl;

import com.k12.user.model.entity.SysRole;
import com.k12.user.mapper.SysRoleDao;
import com.k12.user.model.entity.SysUser;
import com.k12.user.service.SysRoleService;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;
/**
 * 角色信息表(SysRole)表服务实现类
 *
 * @author makejava
 * @since 2025-07-28 20:53:09
 */
@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {
    @Resource
    private SysRoleDao sysRoleDao;

    /**
     * 通过ID查询单条数据
     *
     * @param roleId 主键
     * @return 实例对象
     */
    @Override
    public SysRole queryById(Long roleId,String tenantId) {
        return this.sysRoleDao.queryById(roleId,tenantId);
    }

    /**
     * 分页查询
     *
     * @param sysRole 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public List<SysRole> queryByPage(SysRole sysRole) {
        return this.sysRoleDao.queryAllByLimit(sysRole);
    }

    /**
     * 新增数据
     *
     * @param sysRole 实例对象
     * @return 实例对象
     */
    @Override
    public SysRole insert(SysRole sysRole) {
        this.sysRoleDao.insert(sysRole);
        return sysRole;
    }

    /**
     * 修改数据
     *
     * @param sysRole 实例对象
     * @return 实例对象
     */
    @Override
    public SysRole update(SysRole sysRole) {
        this.sysRoleDao.update(sysRole);
        return this.queryById(sysRole.getRoleId(), String.valueOf(sysRole.getTenantId()));
    }

    /**
     * 通过主键删除数据
     *
     * @param roleId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long roleId) {
        return this.sysRoleDao.deleteById(roleId) > 0;
    }

    @Override
    public String checkRole(SysRole sysRole) {
        SysRole sysRole1 = new SysRole();
        sysRole1.setRoleName(sysRole.getRoleName());
        List<SysRole> sysRoles1 = this.sysRoleDao.queryAllByLimit(sysRole1);
        if (!sysRoles1.isEmpty()){
            return "角色名称已存在";
        }
        SysRole sysRole2 = new SysRole();
        sysRole2.setRoleKey(sysRole.getRoleKey());
        List<SysRole> sysRoles2 = this.sysRoleDao.queryAllByLimit(sysRole2);
        if (!sysRoles2.isEmpty()){
            return "角色权限已存在";
        }
        return "";
    }

    @Override
    public List<SysRole> getAllRoles(String tenantId) {
        SysRole sysRole = new SysRole();
        sysRole.setTenantId(Long.parseLong(tenantId));
        return this.sysRoleDao.queryAllByLimit(sysRole);
    }

    @Override
    public Boolean deleteRoleByIds(Long[] roleIds, String tenantId) {
        return this.sysRoleDao.deleteRoleByIds(roleIds,tenantId) > 0;
    }

    @Override
    public List<SysUser> selectAllocatedList(SysUser sysUser) {
        return this.sysRoleDao.selectAllocatedList(sysUser);
    }

    @Override
    public List<SysUser> unallocatedList(SysUser sysUser) {
        return this.sysRoleDao.unallocatedList(sysUser);
    }


}
